/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 19-12-5 上午11:41
 * History:
 */
package com.swak.frame.api.filter.spi;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swak.frame.chain.FilterInvoker;
import com.swak.frame.context.BoundContext;
import com.swak.frame.dto.base.SysRestCode;
import com.swak.frame.exception.ApiExceptionUtil;
import com.swak.frame.filter.ApiFilter;

/**
 * RequiredLoginFilter.java
 * 
 * @author ColleyMa
 * @version 19-12-5 上午11:41
 */
public class RequiredLoginFilter implements ApiFilter {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public void doFilter(BoundContext context, FilterInvoker<BoundContext> nextFilter) {
		logger.info("start doFilter:{}", DateTime.now().toString());

		boolean requiredLogin = context.isRequiredLogin();
		if (requiredLogin && StringUtils.isEmpty(context.getRequest().getPin())) {
			logger.warn("当前请求必须登录才能访问,请确保是否登录!");
			ApiExceptionUtil.throwException(SysRestCode.USER_NO_LOGIN);
		}
		
		//下一个流程
		nextFilter.invoke(context);
	}
}
